<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="handleClose"
    :title="
      formData.id ? $t('powerGroup.编辑权限组') : $t('powerGroup.新增权限组')
    "
    width="600"
  >
    <LangTabs v-model="lang" />
    <el-form :model="formData" label-width="200px" ref="formRef" :rules="rules">
      <el-form-item :label="$t('powerGroup.权限组名称')" :prop="'name' + lang">
        <el-input
          v-model="formData['name' + lang]"
          maxlength="20"
          show-word-limit
        />
      </el-form-item>
      <el-form-item
        :label="$t('powerGroup.权限组描述')"
        :prop="'description' + lang"
      >
        <el-input
          v-model="formData['description' + lang]"
          type="textarea"
          rows="4"
          show-word-limit
          maxlength="50"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script setup>
import { ref, watch, computed } from "vue";
import Dialog from "@/components/dialog.vue";
import Api from "@/config/api/api";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";
import LangTabs from "@/components/LangTabs.vue";
import { setAttrLangs } from "@/i18n/tools";

const { t, locale } = useI18n();
const lang = ref(locale.value);

const show = defineModel();
const emit = defineEmits(["success"]);
const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});

watch(show, (val) => {
  if (val && props.data) {
    formData.value = JSON.parse(JSON.stringify(props.data));
  }
});

const formData = ref({
  id: "",
  // name: "",
  // description: "",
});

const rules = ref({
  // name: [{ required: true, message: t('powerGroup.请输入权限组名称'), trigger: "blur" }],
  // description: [{ required: true, message: t('powerGroup.请输入权限组描述'), trigger: "blur" }],
});

setAttrLangs(formData.value, "name", "");
setAttrLangs(formData.value, "description", "");
setAttrLangs(rules.value, "name", [
  {
    required: true,
    message: t("powerGroup.请输入权限组名称"),
    trigger: "blur",
  },
]);
setAttrLangs(rules.value, "description", [
  {
    required: true,
    message: t("powerGroup.请输入权限组描述"),
    trigger: "blur",
  },
]);

const formRef = ref();

const handleSubmit = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (formData.value.id) {
          // 编辑权限组
          await Api.updateRole(formData.value);
          ElMessage.success(t("powerGroup.编辑成功"));
        } else {
          // 新增权限组
          await Api.addRole(formData.value);
          ElMessage.success(t("powerGroup.新增成功"));
        }
        emit("success");
        handleClose();
      } catch (error) {
        ElMessage.error(
          formData.value.id
            ? t("powerGroup.编辑失败")
            : t("powerGroup.新增失败")
        );
        console.error(error);
      }
    }
  });
};

const handleClose = () => {
  formRef.value?.resetFields();
  lang.value = locale.value;
  formData.value = {
    id: "",
    // name: "",
    // description: "",
  };
  setAttrLangs(formData.value, "name", "");
  setAttrLangs(formData.value, "description", "");
  show.value = false;
};
</script>
